use serde::{Deserialize, Serialize};
use crate::config;

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all="camelCase")]
pub struct Token {
    name: String,
    value: String,
    is_login: bool,
    login_id: String,
    login_type: String,
    login_device_type: DeviceType,
    expire: Option<u64>,
    active_expire: Option<i64>,
}

impl Token {
    pub fn new(login_id: String, login_type: String, value: String, is_login: bool, login_device_type: DeviceType) -> Self {
        let token_config = config::get().satoken();

        Self {
            name: String::from(token_config.name()),
            value, is_login, login_id, login_type, login_device_type,
            expire: Some(token_config.timeout()),
            active_expire: Some(token_config.active_timeout()),
        }
    }

    pub fn from(token: Token) -> Self {
        Self {
            ..token
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub enum DeviceType {
    PC,
    Mobil
}